package com.example.videoworks.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.videoworks.common.Page;
import com.example.videoworks.common.PageParam;
import com.example.videoworks.controller.WorkInController;
import com.example.videoworks.controller.request.GetListRequest;
import com.example.videoworks.entity.Principal;
import com.example.videoworks.entity.Work;
import com.example.videoworks.entity.WorkIn;
import com.example.videoworks.mapper.PrincipalMapper;
import com.example.videoworks.mapper.WorkInMapper;
import com.example.videoworks.mapper.WorkMapper;
import com.example.videoworks.service.IWorkInService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Lily
 * @since 2023-05-11
 */
@Service
public class WorkInServiceImpl extends ServiceImpl<WorkInMapper, WorkIn> implements IWorkInService {

    @Autowired
    private WorkInMapper workInMapper;
    @Autowired
    private PrincipalMapper principalMapper;
    @Autowired
    private WorkMapper workMapper;

    @Override
    public Page<WorkIn> getPrincipalInWork(GetListRequest request, String workId) {
        if (request == null) return null;
        PageParam pageParam = request.getPageParam();
        PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getOrderBy()/*"uploadTime asc"*/);//asc顺序 desc倒序

        QueryWrapper<WorkIn> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("workId", workId);
        List<WorkIn> list = workInMapper.selectList(queryWrapper);
        QueryWrapper<Principal> queryWrapper1 = new QueryWrapper<>();
        QueryWrapper<Work> queryWrapper2 = new QueryWrapper<>();
        for (WorkIn w: list){
            queryWrapper1.eq("personId", w.getPersonId());
            queryWrapper2.eq("workId", workId);
            w.setPrincipal(principalMapper.selectOne(queryWrapper1));
            w.setWorks(workMapper.selectList(queryWrapper2));
        }
        return new Page(new PageInfo<>(list));

    }
}
